package com.andnux.android.adapter;

import android.view.View;

import com.andnux.android.MainApp;
import com.andnux.android.data.ChatMessage;
import com.andnux.android.data.SystemUsers;
import com.andnux.android.databinding.ItemMessageBinding;
import com.andnux.android.utils.ImageLoader;

public class ChatAdapter extends BaseAdapter<ChatMessage, ItemMessageBinding> {

    private final SystemUsers fromUsers;
    private final SystemUsers toUsers;

    public ChatAdapter(SystemUsers to) {
        this.toUsers = to;
        fromUsers = MainApp.getApp().getSystemUsers();
    }

    @Override
    public void onBindView(ItemMessageBinding binding, ChatMessage chatMessage, int position) {
        super.onBindView(binding, chatMessage, position);
        if (chatMessage.getFromId().equalsIgnoreCase(String.valueOf(fromUsers.getId()))) {
            binding.from.setVisibility(View.VISIBLE);
            binding.to.setVisibility(View.GONE);
            ImageLoader.load(fromUsers.getAvatar(), binding.fromHeadImage);
            binding.fromContent.setText(chatMessage.getMessage());
        } else {
            binding.to.setVisibility(View.VISIBLE);
            binding.from.setVisibility(View.GONE);
            ImageLoader.load(toUsers.getAvatar(), binding.toHeadImage);
            binding.toContent.setText(chatMessage.getMessage());
        }
    }
}
